package cn.bigcontainer.alt.workflow.dto;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.workflow.mapper.MTaskDefinitionMapper;
import cn.bigcontainer.alt.workflow.po.MTaskDefinition;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MWorkflow extends MCrud {

    private List<MTaskDefinitionDto> tasks;

    private void initTasks (MApp app) {
        MTaskDefinitionMapper taskDefinitionMapper = WoKit.getBean(MTaskDefinitionMapper.class);
        List<MTaskDefinition> taskPos = taskDefinitionMapper.selectList(new QueryWrapper<MTaskDefinition>().eq("process_definition_id", app.getId()));
        tasks = WoKit.getDtos(MTaskDefinitionDto.class, taskPos);
        for (MTaskDefinitionDto task : tasks) {
            task.setFormView(getView(task.getFormViewId()));
        }
    }

    public MWorkflow(MApp app) {
        super(app);
        initTasks(app);
    }

    public MWorkflow(MApp app, BigMap<String, Object> req) {
        super(app, req);
        initTasks(app);
    }

    public MWorkflow() {
        super();
    }

    public List<MTaskDefinitionDto> getTasks() {
        return tasks;
    }

    public void setTasks(List<MTaskDefinitionDto> tasks) {
        this.tasks = tasks;
    }

    public MTaskDefinitionDto getTask (String taskId) {
        return tasks.stream().filter(t -> t.getId().equals(taskId)).findFirst().get();
    }
}
